package Facebook;

import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import java.util.Date;

import com.restfb.BinaryAttachment;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;


public class GraphPublisherExample extends Example {
  /**
   * RestFB Graph API client.
   */
  private final FacebookClient facebookClient;

  public static void main(String[] args) {
    	
    new GraphPublisherExample("AAAIO62GqZA2ABAIbffeoyQ5mVXHZCHPiE4llcBUAXlrFZBVBlgZBhigDh924tZAWvTBFNZCMuOByZCZBHeDMZBoatCkZBynuLJKcxxvPCH4o8xywZDZD");

  }

  public GraphPublisherExample(String accessToken) {
    facebookClient = new DefaultFacebookClient(accessToken);
  }

 public void runEverything() {
  // String messageId = publishMessage();
    //delete(messageId);AAAIO62GqZA2ABAIbffeoyQ5mVXHZCHPiE4llcBUAXlrFZBVBlgZBhigDh924tZAWvTBFNZCMuOByZCZBHeDMZBoatCkZB
    //String eventId = publishEvent();
    //delete(eventId);
   //String photoId = publishPhoto();
    //delete(photoId);
  }

  public String publishMessage(String msg) {
    out.println("* Feed publishing *");

    FacebookType publishMessageResponse =
        facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", msg));

    out.println("Published message ID: " + publishMessageResponse.getId());
    return publishMessageResponse.getId();
  }

  public String publishEvent() {
    out.println("* Event publishing *");

    Date tomorrow = new Date(currentTimeMillis() + 1000L * 60L * 60L * 24L);
    Date twoDaysFromNow = new Date(currentTimeMillis() + 1000L * 60L * 60L * 48L);

    FacebookType publishEventResponse =
        facebookClient.publish("me/events", FacebookType.class, Parameter.with("name", "Party"),
          Parameter.with("start_time", tomorrow), Parameter.with("end_time", twoDaysFromNow));

    out.println("Published event ID: " + publishEventResponse.getId());
    return publishEventResponse.getId();
  }
public String chemin (String ch)
{
   ch=(String)ch.subSequence(48,ch.length());

    out.println(ch);
    return ch;
}
public String chemin2 (String ch)
{
   ch=(String)ch.subSequence(49,ch.length());

    out.println(ch);
    return ch;
}
  public String publishPhoto(String ch) {
    out.println("* Binary file publishing *   ");

    FacebookType publishPhotoResponse =facebookClient.publish("me/photos", FacebookType.class,
          BinaryAttachment.with(chemin(ch), getClass().getResourceAsStream("/image/"+chemin(ch))),
          Parameter.with("message", "Testez ce plat Miam Miam"));

    out.println("Published photo ID: " + publishPhotoResponse.getId());
    return publishPhotoResponse.getId();
  }

  public void publishVid(String ch)
  {
      facebookClient.publish("me/videos", FacebookType.class,
  BinaryAttachment.with(chemin2(ch), getClass().getResourceAsStream("/videos/"+chemin2(ch))),
  Parameter.with("message", "Video de Cuisine"));
  }


  public void delete(String objectId) {
    out.println("* Object deletion *");
    out.println(format("Deleted %s: %s", objectId, facebookClient.deleteObject(objectId)));
  }
}